package com.labraff.todo;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class taskList_adapter extends BaseAdapter  {
    private List<task> data;
    private Context mContext;
    int taskType;
    globalApplication app;

    public taskList_adapter(Context mContext, List<task> data,int taskType) {
        super();
        this.mContext = mContext;
        this.data = data;
        this.taskType = taskType;
        app = (globalApplication) mContext.getApplicationContext();
    }
    @Override
    public int getCount() {
        return data.size();
    }
    @Override
    public task getItem(int position) {
        return (task) data.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view= inflater.inflate(R.layout.todolistview_item,null);
        CheckBox todo_check = view.findViewById(R.id.todo_check);
        TextView todo_time = view.findViewById(R.id.todo_time);
        Button todo_edit = view.findViewById(R.id.todo_edit);
        TextView todo_title =view.findViewById(R.id.todo_title);
        if(this.taskType==1){
            todo_title.setVisibility(View.VISIBLE);
            todo_check.setVisibility(View.GONE);
            todo_edit.setVisibility(View.GONE);
        }else {
            todo_title.setVisibility(View.GONE);
            todo_check.setVisibility(View.VISIBLE);
        }
        todo_title.setText(data.get(position).getTitle());
        todo_check.setText(data.get(position).getTitle());
        todo_time.setText(data.get(position).getCreateTime());

        todo_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(todo_check.isChecked()){
                    Toast.makeText(mContext,"完成任务："+data.get(position).getTitle(),Toast.LENGTH_LONG).show();
                    data.remove(position);
//                    TODO 修改任务状态
//                    app.
                }
            }
        });

        todo_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                task currentTask = getItem(position);
                Intent intent = new Intent(mContext,TaskEdit.class);
                intent.putExtra("currentTask",currentTask);
                mContext.startActivity(intent);
                Toast.makeText(mContext,"我丢！",Toast.LENGTH_LONG).show();
            }
        });
        return view;
    }

}
